<?php
declare (strict_types = 1);

namespace app\superAdmin\controller;

use app\BaseController;
use think\facade\Db;
use think\Request;

class user extends BaseController
{
    //显示列表首页
    public function index(Request $request)
    {
        //获取搜索的条件
        $user = session('user');
        $search = $request->all();
        $username =isset($search['username']) ? $search['username']:'';
        $start =isset($search['start']) ? $search['start']:'';
        $end =isset($search['end']) ? $search['end']:'';
        if($user['group_id'] == 1){
            //这里必须使用别名  否则报错  原因未知
            $data = Db::name('user')->alias('a')->field('a.*,b.role')->join("group b","a.group_id=b.id")->where(function($query) use($request){
                //方法内部重新获取搜索条件，方法内部变量与外部变量不互通
                $search = $request->all();
                $start =isset($search['start']) ? $search['start']:'';
                $start = strtotime($start);
                $end =isset($search['end']) ? $search['end']:'';
                $end = strtotime($end);
                $username =isset($search['username']) ? $search['username']:'';
                //根据条件执行SQL
                if($username){
                    $query->where('username','like','%'.$username.'%');
                }
                if(!empty($start) || !empty($end)){
                    $query->where('updated_at','>',$start);
                    $query->where('updated_at','<',$end);
                }
            })->paginate(10);
        }else{
            //这里必须使用别名  否则报错  原因未知
            $data = Db::name('user')->alias('a')->field('a.*,b.role')->join("group b","a.group_id=b.id")->where(function($query) use($request){
                //方法内部重新获取搜索条件，方法内部变量与外部变量不互通
                $search = $request->all();
                $start =isset($search['start']) ? $search['start']:'';
                $start = strtotime($start);
                $end =isset($search['end']) ? $search['end']:'';
                $end = strtotime($end);
                $username =isset($search['username']) ? $search['username']:'';
                //根据条件执行SQL
                if($username){
                    $query->where('username','like','%'.$username.'%');
                }
                if(!empty($start) || !empty($end)){
                    $query->where('updated_at','>',$start);
                    $query->where('updated_at','<',$end);
                }
            })->where('a.id',$user['id'])->paginate(10);
        }
        //单独拿出分页变量
        $page = $data -> render();
        return view('',['data'=>$data,'page'=>$page,'search'=>$username,'start'=>$start,'end'=>$end]);
    }

    //显示添加页面
    public function create()
    {
        $group = Db::name("group") ->field('id,role')->select();
        return view('',['group'=>$group]);
    }

    //显示编辑页面
    public function edit(Request $request)
    {
        $get = $request -> all();
        $data = Db::name('user')->find($get['id']);
        $group = Db::name("group") ->field('id,role')->select();
        return view('',['data'=>$data,'group'=>$group]);
    }
}
